ProductsIntegrationsResourcesDocumentationPricing
Start Now

© 2026 CapSolver. All rights reserved.

CONTACT US

Slack: lola@capsolver.com

Products

  • reCAPTCHA v2
  • reCAPTCHA v3
  • Cloudflare Turnstile
  • Cloudflare Challenge
  • AWS WAF
  • Browser Extension
  • Many more CAPTCHA types

Integrations

  • Selenium
  • Playwright
  • Puppeteer
  • n8n
  • Partners
  • View All Integrations

Resources

  • Referral System
  • Documentation
  • API Reference
  • Blog
  • FAQs
  • Glossary
  • Status

Legal

  • Terms & Conditions
  • Privacy Policy
  • Refund Policy
  • Don't Sell My Info
Blog/The other captcha/How to Solve Image captcha with Python
Sep21, 2023

How to Solve Image captcha with Python

Ethan Collins

Ethan Collins

Pattern Recognition Specialist


Introduction

Image-to-Text captchas are one of the most common forms of bot verification on the internet. They present an image containing distorted, warped, or obscured characters and require the user to correctly identify and enter the text to prove they are human. For developers working on web scraping or automation, these captchas are a widespread obstacle.

Fortunately, with AI-powered recognition services, this process can be easily automated. This guide will show you how to use Python and the CapSolver service to solve image captchas quickly and accurately.

⚙️ Prerequisites

Before you begin, make sure you have the following:

  • Python installed: Ensure Python is set up on your system.
  • CapSolver API Key: You will need a CapSolver account to get your API key from the dashboard.

🤖 Step 1: Install Necessary Python Packages

First, you need to install the official CapSolver Python library. This package simplifies interaction with the CapSolver API. Open your terminal and run the following command:

bash Copy
pip install capsolver

👨‍💻 Step 2: Write Python Code to Solve Image Captcha

The following Python script demonstrates how to solve an image captcha using CapSolver. The core of this code is the ImageToTextTask, which leverages CapSolver's powerful Vision Engine to recognize the text within an image.

Updated Python Code

This code is more modular and includes functions to handle both local image files and image URLs, making it more versatile for different use cases.

python Copy
import capsolver
import base64
import requests
from io import BytesIO

# -------------------PLEASE MODIFY THESE VALUES-------------------
# Your API key from the CapSolver Dashboard
CAPSOLVER_API_KEY = "Your_API_KEY"
# ----------------------------------------------------------------

def setup_capsolver():
    """Sets the CapSolver API key."""
    capsolver.api_key = CAPSOLVER_API_KEY

def get_base64_from_image(source, is_url=False):
    """
    Converts an image file or URL into a Base64 encoded string.
    :param source: The path (for a local file) or URL of the image.
    :param is_url: True if the source is a URL.
    :return: A Base64 encoded string, or None on failure.
    """
    try:
        if is_url:
            response = requests.get(source)
            response.raise_for_status()  # Raise an exception for bad status codes
            image_bytes = BytesIO(response.content)
        else:
            with open(source, 'rb') as image_file:
                image_bytes = BytesIO(image_file.read())
        
        base64_string = base64.b64encode(image_bytes.read()).decode('utf-8')
        return base64_string
    except requests.exceptions.RequestException as e:
        print(f"Could not fetch image from URL: {e}")
        return None
    except FileNotFoundError:
        print(f"Error: File not found at {source}")
        return None
    except Exception as e:
        print(f"An error occurred converting image to Base64: {e}")
        return None

def solve_image_captcha(base64_image, module_name="common"):
    """
    Solves an image captcha using CapSolver.
    :param base64_image: The Base64 encoded image string.
    :param module_name: The recognition module, 'common' for general purpose.
    :return: The solution object, or None on failure.
    """
    print("Submitting image captcha task to CapSolver...")
    try:
        solution = capsolver.solve({
            "type": "ImageToTextTask",
            "module": module_name,
            "body": base64_image
        })
        return solution
    except Exception as e:
        print(f"An error occurred while solving the captcha: {e}")
        return None

def main():
    """Main function to execute the process."""
    setup_capsolver()

    # --- Example 1: Using a local image file ---
    print("--- Example 1: Using a local image file ---")
    local_image_path = "path/to/your/captcha.jpg"  # Replace with your image path
    base64_from_file = get_base64_from_image(local_image_path)
    
    if base64_from_file:
        solution = solve_image_captcha(base64_from_file)
        if solution:
            print("Solution Text:", solution.get("text"))

    print("\n" + "="*30 + "\n")

    # --- Example 2: Using an image URL ---
    print("--- Example 2: Using an image URL ---")
    image_url = "https://i.postimg.cc/B6hK2V19/captcha-example.png" # Example image URL
    base64_from_url = get_base64_from_image(image_url, is_url=True)

    if base64_from_url:
        solution = solve_image_captcha(base64_from_url)
        if solution:
            print("Solution Text:", solution.get("text"))


if __name__ == "__main__":
    main()

⚠️ Important Variables to Change

Before running the code, be sure to modify the following:

  • CAPSOLVER_API_KEY: Find your API key in the CapSolver Dashboard and replace the placeholder.
  • local_image_path (in the main function): If you want to test a local image, replace this with the actual file path to your captcha image.

Conclusion

By integrating CapSolver into your Python scripts, you can fully automate the process of recognizing image captchas. This method is not only efficient and accurate but also frees you from the repetitive manual labor of solving these challenges. It's a critical efficiency boost for any project involving large-scale data collection or automated testing. If you're interested in tackling more complex captcha types, you can learn how to solve reCAPTCHA v2 with Playwright, which demonstrates handling dynamic challenges in a browser automation context.

Frequently Asked Questions (FAQ)

Q1: How accurate is CapSolver's recognition?
A1: CapSolver uses advanced AI models and achieves a high accuracy rate for most standard image captchas. The accuracy can vary depending on the complexity of the captcha (e.g., level of distortion, background noise).

Q2: What is the module parameter for?
A2: The module parameter allows you to specify a particular recognition engine to optimize performance for a certain type of captcha. common is the general-purpose module suitable for most cases. For more specialized modules, refer to the official ImageToTextTask documentation.

Q3: Can I use this method directly in my web scraper?
A3: Absolutely. Once you have the recognized text, you can use it to fill out the captcha form and proceed with your scraping or automation workflow. This method can be seamlessly integrated into any Python project.

Q4: Does CapSolver support languages other than Python?
A4: Yes. CapSolver provides libraries and API endpoints for multiple programming languages, including Node.js, Go, and others, making it easy to integrate into virtually any tech stack.

More

The other captchaApr 14, 2026

Can AI Solve CAPTCHA? How Detection and Solve Really Work

Explore how AI detects and solves CAPTCHA challenges, from image recognition to behavioral analysis. Understand the technology behind AI CAPTCHA solvers and how CapSolver aids automated workflows. Learn about the evolving battle between AI and human verification.

Sora Fujimoto
Sora Fujimoto
The other captchaApr 09, 2026

CAPTCHA Solving API Performance Comparison: Speed, Accuracy & Cost (2026)

Compare top CAPTCHA solving APIs by speed, accuracy, uptime, and pricing. See how CapSolver, 2Captcha, CapMonster Cloud, and others stack up in our detailed performance comparison.

Contents

Aloísio Vítor
Aloísio Vítor
The other captchaApr 07, 2026

How to Integrate CAPTCHA Solving API in Python: Step-by-Step Guide

Master how to integrate CAPTCHA solving API in Python with this step-by-step guide. Learn to automate reCAPTCHA, Geetest, and AWS WAF using CapSolver for reliable data extraction.

Aloísio Vítor
Aloísio Vítor
The other captchaApr 03, 2026

Image Recognition API for Custom CAPTCHAs: How It Works in Automation

Discover how an Image Recognition API for custom CAPTCHAs streamlines automation. Learn about AI vision logic, OCR vs. AI, and CapSolver's modular solutions.

Rajinder Singh
Rajinder Singh